package cn.yhjz.platform.system.controller;

import cn.yhjz.platform.system.service.ISysRoleMenuService;
import cn.yhjz.platform.system.entity.SysRoleMenu;
import cn.yhjz.platform.system.vo.SysRoleMenuVo;
import cn.yhjz.common.AjaxResponse;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/system/sysRoleMenu")
@PreAuthorize("hasAuthority('system:role')")
public class SysRoleMenuController {

    @Autowired
    private ISysRoleMenuService sysRoleMenuService;

    @PostMapping("/list")
    public List<SysRoleMenuVo> list(@RequestBody SysRoleMenu sysRoleMenu) {
        return sysRoleMenuService.select(sysRoleMenu);
    }

    @PostMapping("/page")
    public AjaxResponse page(@RequestBody SysRoleMenuVo vo) {
        SysRoleMenu entity = new SysRoleMenu();
        BeanUtils.copyProperties(vo, entity);
        return AjaxResponse.success(sysRoleMenuService.getPage(vo.getCurrentPage(), vo.getPageSize(), entity));
    }

    @PostMapping("/addOrUpdate")
    public AjaxResponse add(@RequestBody SysRoleMenu entity) {
        if (entity.getId() != null) {
            return AjaxResponse.success(sysRoleMenuService.updateById(entity));
        } else {
            return AjaxResponse.success(sysRoleMenuService.insert(entity));
        }
    }

    @PostMapping("/setByRole")
    public AjaxResponse setByRole(String roleCode, @RequestBody List<SysRoleMenu> sysRoleMenuList) {
        return AjaxResponse.success(sysRoleMenuService.setByRole(roleCode, sysRoleMenuList));
    }

    @GetMapping("/selectById")
    public AjaxResponse selectById(Long id) {
        return AjaxResponse.success(sysRoleMenuService.selectById(id));
    }

    @DeleteMapping("/deleteById")
    public AjaxResponse deleteById(Long id) {
        return AjaxResponse.success(sysRoleMenuService.deleteById(id));
    }

    @RequestMapping("/loadByRoleCode")
    public AjaxResponse loadByRoleCode(String roleCode) {
        SysRoleMenu param = new SysRoleMenu();
        param.setRoleCode(roleCode);
        return AjaxResponse.success(sysRoleMenuService.select(param));
    }
}
